<template>
    <div>
        <p>计时器 ticks: {{ ticks }}</p>
    </div>
</template>

<script>
import Worker from 'worker-loader!./worker.js';
export default {
    data() {
        return {
            ticks: 0,
            worker: null,
        };
    },
    mounted() {
        this.worker = new Worker();
        this.worker.postMessage({ type: 'start', interval: 1000 });
        this.worker.onmessage = this.cbDo;
    },
    beforeDestroy() {
        if (this.worker) {
            this.worker.terminate();
        }
    },
    methods: {
        cbDo(msg) {
            // 计时到，你要做的事
            this.ticks += 1;
            alert(this.ticks)
        },
    },
};
</script>
